package com.example.demo.controller;


import com.example.demo.annotation.UserLoginToken;
import com.example.demo.service.InfoService;
import com.example.demo.service.UserService;
import com.example.demo.utils.Result;
import com.example.demo.vo.InfoVo;
import com.example.demo.vo.userVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/user")
public class userController {
    @Autowired
    private InfoService infoService;

    @Autowired
    private UserService userservice;

    @RequestMapping("/getuserInfo")
    @ResponseBody
    public Result getuserInfo(){
        List<InfoVo> infoVo = infoService.getInfo();
        return Result.success(infoVo);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "/Login")
    public Result Login(@RequestBody userVo user){
        System.err.println(user);
        return userservice.Login(user);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "/register")
    public Result register(userVo user){
        System.err.println(user);
        return userservice.register(user);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "/add")
    public Result add(@RequestBody userVo user){
        return userservice.register(user);
    }

    @ResponseBody
    @UserLoginToken
    @RequestMapping("list")
    public Result getList(@RequestParam(required = false) String username,
                          @RequestParam(required = false) Integer id ,
                          int currentPage,
                          int pageSize){
        return userservice.getlist(username,id,currentPage,pageSize);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "/del")
    public Result del(@RequestParam Integer id){
        return userservice.del(id);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value = "/edit")
    public Result edit(@RequestBody userVo uservo){
        System.err.println(uservo.getId());
        return userservice.update(uservo);
    }

}
